package com.system.Service.Impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.common.Entity.Result;
import com.system.Entity.OssContent;
import com.system.Service.ImageService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Service
public class ImageServiceImpl implements ImageService {
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.access-key-id}")
    private String accessKeyId;

    @Value("${aliyun.oss.access-key-secret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucket-name}")
    private String bucketName;
    @Override
    public String upload_show(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        //取文件后缀
        String suffix = null;
        if (originalFilename != null) {
            suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        String newFileName = UUID.randomUUID().toString() + suffix;
        String objectName = "ShowPoster/" + newFileName;

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        String urlPrefix = "https://" + bucketName + "." + endpoint;

        try (InputStream inputStream = file.getInputStream()) {
            // 上传文件到OSS
            ossClient.putObject(bucketName, objectName, inputStream);


            return urlPrefix + "/" + objectName;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public void deleteImage_show(String url) {
        String objectName = url.substring(url.lastIndexOf("/") + 1);
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 删除图片
        ossClient.deleteObject(bucketName, "ShowPoster/"+objectName);
    }


    @Override
    public String upload_program(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        //取文件后缀
        String suffix = null;
        if (originalFilename != null) {
            suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        String newFileName = UUID.randomUUID().toString() + suffix;
        String objectName = "ProgramPoster/" + newFileName;

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        String urlPrefix = "https://" + bucketName + "." + endpoint;

        try (InputStream inputStream = file.getInputStream()) {
            // 上传文件到OSS
            ossClient.putObject(bucketName, objectName, inputStream);

            // 获取头像URL
            return urlPrefix + "/" + objectName;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public void deleteImage_program(String url) {
        String objectName = url.substring(url.lastIndexOf("/") + 1);
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 删除图片
        ossClient.deleteObject(bucketName, "ProgramPoster/"+objectName);
    }

    @Override
    public String upload_schoolfounder(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        //取文件后缀
        String suffix = null;
        if (originalFilename != null) {
            suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        String newFileName = UUID.randomUUID().toString() + suffix;
        String objectName = "SchoolFounderImage/" + newFileName;

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        String urlPrefix = "https://" + bucketName + "." + endpoint;

        try (InputStream inputStream = file.getInputStream()) {
            // 上传文件到OSS
            ossClient.putObject(bucketName, objectName, inputStream);

            // 获取头像URL
            return urlPrefix + "/" + objectName;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public void deleteImage_schoolfounder(String url) {
        String objectName = url.substring(url.lastIndexOf("/") + 1);
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 删除图片
        ossClient.deleteObject(bucketName, "SchoolFounderImage/"+objectName);
    }

    @Override
    public String upload_artist(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        //取文件后缀
        String suffix = null;
        if (originalFilename != null) {
            suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        String newFileName = UUID.randomUUID().toString() + suffix;
        String objectName = "ArtistImage/" + newFileName;

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        String urlPrefix = "https://" + bucketName + "." + endpoint;

        try (InputStream inputStream = file.getInputStream()) {
            // 上传文件到OSS
            ossClient.putObject(bucketName, objectName, inputStream);

            // 获取头像URL
            return urlPrefix + "/" + objectName;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public void deleteImage_artist(String url) {
        String objectName = url.substring(url.lastIndexOf("/") + 1);
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 删除图片
        ossClient.deleteObject(bucketName, "ArtistImage/"+objectName);
    }

    @Override
    public String upload_artistWork(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        //取文件后缀
        String suffix = null;
        if (originalFilename != null) {
            suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        String newFileName = UUID.randomUUID().toString() + suffix;
        String objectName = "ArtistWorkImage/" + newFileName;

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        String urlPrefix = "https://" + bucketName + "." + endpoint;

        try (InputStream inputStream = file.getInputStream()) {
            // 上传文件到OSS
            ossClient.putObject(bucketName, objectName, inputStream);

            // 获取头像URL
            return urlPrefix + "/" + objectName;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public void deleteImage_artistWork(String url) {
        String objectName = url.substring(url.lastIndexOf("/") + 1);
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 删除图片
        ossClient.deleteObject(bucketName, "ArtistWorkImage/"+objectName);
    }

    @Override
    public String upload_artistHistory(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        //取文件后缀
        String suffix = null;
        if (originalFilename != null) {
            suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        String newFileName = UUID.randomUUID().toString() + suffix;
        String objectName = "ArtistHistoryImage/" + newFileName;

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        String urlPrefix = "https://" + bucketName + "." + endpoint;

        try (InputStream inputStream = file.getInputStream()) {
            // 上传文件到OSS
            ossClient.putObject(bucketName, objectName, inputStream);

            // 获取头像URL
            return urlPrefix + "/" + objectName;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public void deleteImage_artistHistory(String url) {
        String objectName = url.substring(url.lastIndexOf("/") + 1);
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 删除图片
        ossClient.deleteObject(bucketName, "ArtistHistoryImage/"+objectName);
    }

    @Override
    public String upload_dramaImage(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        //取文件后缀
        String suffix = null;
        if (originalFilename != null) {
            suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        String newFileName = UUID.randomUUID().toString() + suffix;
        String objectName = "Drama/"+"Image/" + newFileName;

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        String urlPrefix = "https://" + bucketName + "." + endpoint;

        try (InputStream inputStream = file.getInputStream()) {
            // 上传文件到OSS
            ossClient.putObject(bucketName, objectName, inputStream);

            // 获取头像URL
            return urlPrefix + "/" + objectName;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public String upload_dramaAudio(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        //取文件后缀
        String suffix = null;
        if (originalFilename != null) {
            suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        String newFileName = UUID.randomUUID().toString() + suffix;
        String objectName = "Drama/"+"Audio/" + newFileName;

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        String urlPrefix = "https://" + bucketName + "." + endpoint;

        try (InputStream inputStream = file.getInputStream()) {
            // 上传文件到OSS
            ossClient.putObject(bucketName, objectName, inputStream);

            // 获取头像URL
            return urlPrefix + "/" + objectName;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public String upload_dramaVideo(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        //取文件后缀
        String suffix = null;
        if (originalFilename != null) {
            suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        String newFileName = UUID.randomUUID().toString() + suffix;
        String objectName = "Drama/"+"Video/" + newFileName;

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        String urlPrefix = "https://" + bucketName + "." + endpoint;

        try (InputStream inputStream = file.getInputStream()) {
            // 上传文件到OSS
            ossClient.putObject(bucketName, objectName, inputStream);

            // 获取头像URL
            return urlPrefix + "/" + objectName;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public String articleImage(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        //取文件后缀
        String suffix = null;
        if (originalFilename != null) {
            suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        String newFileName = UUID.randomUUID().toString() + suffix;
        String objectName = "Article/"+"Image/" + newFileName;

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        String urlPrefix = "https://" + bucketName + "." + endpoint;

        try (InputStream inputStream = file.getInputStream()) {
            // 上传文件到OSS
            ossClient.putObject(bucketName, objectName, inputStream);

            // 获取头像URL
            return urlPrefix + "/" + objectName;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public String articleVideo(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        //取文件后缀
        String suffix = null;
        if (originalFilename != null) {
            suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        String newFileName = UUID.randomUUID().toString() + suffix;
        String objectName = "Article/"+"Video/" + newFileName;

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        String urlPrefix = "https://" + bucketName + "." + endpoint;

        try (InputStream inputStream = file.getInputStream()) {
            // 上传文件到OSS
            ossClient.putObject(bucketName, objectName, inputStream);

            // 获取头像URL
            return urlPrefix + "/" + objectName;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public OssContent getCredentials() {
        OssContent ossContent = new OssContent();
        ossContent.setAccessKeyId(accessKeyId);
        ossContent.setAccessKeySecret(accessKeySecret);
        ossContent.setEndpoint(endpoint);
        ossContent.setBucketName(bucketName);
        return ossContent;
    }

    @Override
    public String post(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        //取文件后缀
        String suffix = null;
        if (originalFilename != null) {
            suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        String newFileName = UUID.randomUUID().toString() + suffix;
        String objectName = "Forum/"+"Post/" + newFileName;

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        String urlPrefix = "https://" + bucketName + "." + endpoint;

        try (InputStream inputStream = file.getInputStream()) {
            // 上传文件到OSS
            ossClient.putObject(bucketName, objectName, inputStream);

            // 获取头像URL
            return urlPrefix + "/" + objectName;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public void deletePost(String url) {
        String objectName = url.substring(url.lastIndexOf("/") + 1);
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 删除图片
        ossClient.deleteObject(bucketName, "Forum/Post/"+objectName);
    }


}
